﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class JobService : IJobService
    {
        private IJobsDal _job;
        private IMapper _mapper;
        public JobService(IJobsDal job, IMapper mapper)
        {
            _job = job;
            _mapper = mapper;
        }

        public async Task<int> AddJobAsync(string title)
        {
            return await _job.AddAsync(new Job 
            {
                Title = title
            });
        }

        public async Task<int> DeleteJobAsync(int id)
        {
            var data = await _job.QueryAsync(id);
            return data == null ? -1 : await _job.DeleteAsync(data);
        }

        public async Task<int> EditJobAsync(int id, string title)
        {
            var data = await _job.QueryAsync(id);
            if (data != null)
                return -1;
            data.Title = title;
            return await _job.EditAsync(data);
        }

        public async Task<List<JobDto>> GetJobAllAsync()
        {
            var data = await _job.QueryAsync();
            return _mapper.Map<List<Job>, List<JobDto>>(data);
        }

        public async Task<JobDto> QueryJobByIdAsync(int id)
        {
            var data = await _job.QueryAsync(id);
            return _mapper.Map<Job, JobDto>(data);
        }

        public async Task<List<JobDto>> QueryJobByNameAsync(string title)
        {
            var data = await _job.QueryAsync(j => j.Title.Contains(title));
            return _mapper.Map<List<Job>,List<JobDto>>(data);
        }
    }
}
